﻿@model TaxonomyFieldViewModel
@using Orchard.Taxonomies.Helpers;
@using Orchard.Utility.Extensions;
@using Orchard.Taxonomies.ViewModels;

@{
    Style.Include("admin-taxonomy.css");
    Script.Require("jQuery");
    Script.Include("~/Themes/TheAdmin/scripts/admin.js").AtFoot();
    Script.Include("admin-taxonomy-expando.js").AtFoot();

    int termIndex = 0;
    var settings = Model.Settings;

    var noSelectionId = Guid.NewGuid();
}

<fieldset class="taxonomy-wrapper" data-name-prefix="@Html.FieldNameFor(m => m)" data-id-prefix="@Html.FieldIdFor(m => m)">
    <legend @if(settings.Required) { <text>class="required"</text> }>@Model.DisplayName</legend>

    <div class="expando">
        @if (!String.IsNullOrWhiteSpace(Model.Settings.Hint)) {
            <span class="hint">@Model.Settings.Hint</span>
        }

    <ul class="terms">
        @if (Model.Settings.SingleChoice && !settings.Required) {
            <li>
                <input type="radio" value="0" @if (Model.SingleTermId == 0) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@noSelectionId" data-term="@T("No selection")" />
                <label class="forcheckbox" for="@noSelectionId">@T("No selection")</label>
            </li>
        }
        @foreach (var entry in Model.Terms) {
            var ti = termIndex;
            <li>
                @* Tabs for levels *@ @for (var i = 1; i <= entry.GetLevels(); i++) {<span class="gap">&nbsp;</span>}
                @{
                    var disabled = !entry.Selectable || (Model.Settings.LeavesOnly && Model.Terms.Any(t => t.Path.Contains(entry.Path + entry.Id)));
                    if (Model.Settings.SingleChoice) {
                        <input @if (disabled) { <text> disabled="disabled" </text> } type="radio" value="@Model.Terms[ti].Id" @if (entry.Id == Model.SingleTermId) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" />
                    } else {
                        <input @if (disabled) { <text> disabled="disabled" </text> } type="checkbox" value="true" @if (entry.IsChecked) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.Terms[ti].IsChecked)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" />
                    }
                }
                @Html.HiddenFor(m => m.Terms[ti].Id)
                <label class="forcheckbox" for="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)">@entry.Name</label>
            </li>
            termIndex++;
        }
    </ul>

         @if (Model.TaxonomyId == 0) {
            <p>@T("You haven't specified a taxonomy for {0}", Model.DisplayName)</p>
        }else if (!Model.Terms.Any() && AuthorizedFor(Orchard.Taxonomies.Permissions.CreateTerm)) {
            <div class="no-terms">
                @T("There are no terms defined for {0} yet.", Model.DisplayName)
                <a href="@Url.Action("Index", "TermAdmin", new { taxonomyId = Model.TaxonomyId, area = "Orchard.Taxonomies" })">@T("Create some terms")</a>
            </div>
        }
    </div>
    @Html.HiddenFor(m => m.TaxonomyId)
</fieldset>
